package annotion.example.hss_fst.baselib.fragments;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

//import com.hss.common.utils.log.LogUtils;
import com.thin.downloadmanager.DefaultRetryPolicy;
import com.thin.downloadmanager.DownloadRequest;
import com.thin.downloadmanager.DownloadStatusListener;
import com.thin.downloadmanager.ThinDownloadManager;

import java.util.LinkedList;
import java.util.List;

/**
 * <b>Project:</b> BaseLib<br>
 * <b>Create Date:</b> 2016/5/3 0003<br>
 * <b>Author:</b> huang saisai <br>
 * <b>Description:</b> <br>
 */
public class DownloadFragment extends ListFragment {
    private ThinDownloadManager downloadManager;
    List<String> list;
    ArrayAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        downloadManager = new ThinDownloadManager();
        list = new LinkedList<>();
        for (int i=0; i<1; i++){
            list.add("download");
        }
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
                                           list);
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.setListAdapter(adapter);
        getListView().setOnItemClickListener(itemClickListener);
    }

    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
                case 0://basicNoTitle
                   download();
                    break;
            }
        }};
    void  download(){
        Uri downloadUri = Uri.parse("http://120.52.73.39/mp4.68mtv.com/mp44/26907-%E9%A9%AC%E6%97%AD%E4%B8%9C-%E5%B0%8F%E6%B5%AA%E6%BC%AB[68mtv.com].mp4");
        Uri destinationUri = Uri.parse(getContext().getExternalCacheDir().toString()+"/test.mp4");
        DownloadRequest downloadRequest = new DownloadRequest(downloadUri)
                .addCustomHeader("Auth-Token", "YourTokenApiKey")
                .setRetryPolicy(new DefaultRetryPolicy())
                .setDestinationURI(destinationUri).setPriority(DownloadRequest.Priority.HIGH)
//                .setDownloadContext(downloadContextObject)//Optional
                .setDownloadListener(new DownloadStatusListener() {
                    @Override
                    public void onDownloadComplete(int id) {

                    }

                    @Override
                    public void onDownloadFailed(int id, int errorCode, String errorMessage) {
//                        LogUtils.e("errorMessage  = "+errorMessage);

                    }

                    @Override
                    public void onProgress(int id, long totalBytes, long downlaodedBytes, int progress) {
//                        LogUtils.e("progress  = "+progress);
                    }
                });
        int downloadId = downloadManager.add(downloadRequest);
//        int status = downloadManager.cancel(downloadId);
//        downloadManager.cancelAll();
//        downloadManager.release();

    }
}
